package com.cuitrip.business.tripservice;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.SuperFragmentUtil;
import android.view.KeyEvent;

import com.cuitrip.business.tripservice.model.ServiceInfo;
import com.cuitrip.service.R;
import com.cuitrip.util.track.TrackUtil;
import com.lab.app.BaseActivity;
import com.lab.app.BaseFragment;
import com.lab.jumper.BundleCenter;
import com.lab.jumper.Jumper;


/**
 * 行程编辑activity
 * Created by AK
 */
public class TripEditActivity extends BaseActivity {

    public static final String SERVICE_INFO_KEY = "service_info";
    public static final String SERVICE_INFO_ID_KEY = "service_info_id";
    public static final String SERVICE_EDIT_MODE_KEY = "service_mode";
    private Bundle mFragmentBundle;
    private TripEditMainFragment mTripMainFragment;


    public static void navigate2CreateTripActivity(Context context) {
        TrackUtil.trackScreenName("/insider/trips/create");
        Intent intent = new Intent(context, TripEditActivity.class);
        Jumper.launchActivity(context, intent);
    }

    public static void navigate2TripEditActivity(Context context, ServiceInfo serviceInfo, boolean isEdit) {
        Intent intent = new Intent(context, TripEditActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(SERVICE_INFO_KEY, serviceInfo);
        bundle.putBoolean(SERVICE_EDIT_MODE_KEY, isEdit);
        intent.putExtras(bundle);
        Jumper.launchActivity(context, intent);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 移除BunderCenter原始Bundle,这里待优化,可能因业务需要而采取原始数据作为后续Bundle传入Fragment
        BundleCenter.getInstance().removeBundle(getIntent());
        if (null != getIntent()) {
            mFragmentBundle = getIntent().getExtras();
        }
        setContentView(R.layout.page_container_layout);
        mTripMainFragment = new TripEditMainFragment();
        mTripMainFragment.setArguments(mFragmentBundle);
        openFragment(mTripMainFragment, R.id.container, TripEditMainFragment.class.getName());
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
                mTripMainFragment.onBackPressed(true);
                return true;
            } else {
                Fragment topFragment = SuperFragmentUtil.getCurrentTopFragment(getSupportFragmentManager());
                if (topFragment != null) {
                    if (topFragment instanceof BaseFragment) {
                        if (((BaseFragment) topFragment).onBackPressed(true)) {
                            return true;
                        } else {
                            try {
                                getSupportFragmentManager().popBackStackImmediate();
                            } catch (Exception e) {
                                // TODO: handle exception
                            }
                            return true;
                        }
                    } else {

                        try {
                            getSupportFragmentManager().popBackStackImmediate();
                        } catch (Exception e) {
                            // TODO: handle exception
                        }
                        return true;
                    }
                } else {

                    try {
                        getSupportFragmentManager().popBackStackImmediate();
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                    return true;
                }
            }
        }else{
            return super.onKeyDown(keyCode, event);
        }
    }

    public enum ContentMode {
        NEW_MODE, ContentMode, EDIT_MODE
    }
}
